﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.NhomTKKeToan
{
    public partial class frmXuLy : Form
    {

        #region Delegate
        public delegate void GetString(Boolean capnhat);
        public GetString GetData;
        private Boolean kiemtrasua = false;
        public static bool CheckClose = false;
        #endregion

        #region Khoi Tao
        private int NhomTKKeToanID;
        private string MaNhomTKKeToan = string.Empty;

        private Entities.NhomTKKeToan nhomTKKT;
        public Entities.NhomTKKeToan NhomTKKT
        {
            get { return nhomTKKT; }
            set { nhomTKKT = value; }
        }
        public frmXuLy()
        {
            InitializeComponent();
            Event();
        }
        private string hanhdong;
        public string Hanhdong
        {
            get { return hanhdong; }
            set { hanhdong = value; }
        }

        // Insert
        public frmXuLy(string hanhdong, Entities.NhomTKKeToan nhomTKKT)
        {
            InitializeComponent();
            Event();
            this.hanhdong = hanhdong;
            this.nhomTKKT = nhomTKKT;
        }
        #endregion

        #region Event
        private void Event()
        {
            btnThoat.MouseDown += new MouseEventHandler(imgThoat_MouseDown);
            this.Closing += new System.ComponentModel.CancelEventHandler(frmXuLyNhomHangHoa_Closing);
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Escape)
                {
                    this.Thoat();
                }
                if (e.KeyCode == Keys.F2)
                {

                    if (this.hanhdong == Common.Constants.GiaTriCacForm.ThemNhomTKKT)
                    {
                        this.Them();
                    }
                    if (this.hanhdong == Common.Constants.GiaTriCacForm.SuaNhomTKKT)
                    {
                        this.Sua();
                    }
                }
            }
            catch 
            {  }
        }

        private void imgThem_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.hanhdong == Common.Constants.Insert)
            {
                this.Them();
            }
            if (this.hanhdong == Common.Constants.Update)
            {
                this.Sua();
            }
        }

        private void imgThoat_MouseDown(object sender, MouseEventArgs e)
        {
            this.Thoat();
        }
        #endregion

        #region FormLoad
        private void frmXuLy_Load(object sender, EventArgs e)
        {
            try
            {
                this.WindowState = FormWindowState.Normal;
                if (this.hanhdong == Common.Constants.Insert)
                {
                    txtMaNhomHang.ReadOnly = false;
                }
                if (this.hanhdong == Common.Constants.Update)
                {
                    txtMaNhomHang.ReadOnly = true;
                    XuLyDuLieuSua();
                }
                txtMaNhomHang.Focus();
                lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap;
            }
            catch 
            {  }
        }

        #endregion

        #region Do du lieu sua
        private void XuLyDuLieuSua()
        {
            try
            {
                NhomTKKeToanID = this.nhomTKKT.NhomTKKeToanID;
                txtMaNhomHang.Text = this.nhomTKKT.MaNhomTKKeToan;
                txtTenNhomHang.Text = this.nhomTKKT.TenNhomTaiKhoan;
                txtGhiChu.Text = this.nhomTKKT.GhiChu;
            }
            catch 
            {  }
        }
        #endregion

        #region CheckPerMission
        private Boolean CheckPerMission()
        {
            return true;
        }
        #endregion

        #region Validate
        private Boolean Validate(Entities.NhomTKKeToan xuly)
        {
            Boolean kt = true;
            try
            {
                try
                {
                    if (txtMaNhomHang.Text != string.Empty)
                    {
                        int ma = int.Parse(txtMaNhomHang.Text);
                    }
                }
                catch 
                {
                    MessageBox.Show("Định dạng không đúng", Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    txtMaNhomHang.Text = "";
                    txtMaNhomHang.Focus();
                    kt = false;
                    return kt;
                }
                if (xuly.MaNhomTKKeToan == string.Empty)
                {
                    kt = false;
                    txtMaNhomHang.Focus();
                    MessageBox.Show(Common.Constants.GiaTriCacForm.NhapMaNhomTKKT, Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    return kt;
                }
                if (xuly.TenNhomTaiKhoan == string.Empty)
                {
                    kt = false;
                    txtTenNhomHang.Focus();
                    MessageBox.Show(Common.Constants.GiaTriCacForm.NhapTenNhomTKKT, Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    return kt;
                }
            }
            catch 
            {  kt = false; }
            return kt;
        }
        #endregion

        #region CheckConflick
        private Entities.NhomTKKeToan SoSanh()
        {
            Entities.NhomTKKeToan sosanh = null;
            try
            {
                Entities.NhomTKKeToan gitri = new Entities.NhomTKKeToan("0");
                Entities.NhomTKKeToan[] nhom = Common.StaticVariable.InterfaceObject.SelectNhomTKKeToan(gitri);

                for (int i = 0; i < nhom.Length; i++)
                {
                    if (nhom[i].MaNhomTKKeToan == this.nhomTKKT.MaNhomTKKeToan)
                    {
                        sosanh = nhom[i];
                    }
                }
            }
            catch 
            {  sosanh = null; }
            return sosanh;
        }

        private Boolean CheckConflick(Entities.NhomTKKeToan manggoc, Entities.NhomTKKeToan sosanh)
        {
            Boolean kt = true;
            try
            {
                this.nhomTKKT = sosanh;
                if (manggoc.MaNhomTKKeToan != sosanh.MaNhomTKKeToan)
                {
                    kt = false;
                    txtMaNhomHang.Text = sosanh.MaNhomTKKeToan;
                    MessageBox.Show(Common.Constants.GiaTriCacForm.NhapMaNhomTKKTTD, Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    return kt;
                }
                if (manggoc.TenNhomTaiKhoan != sosanh.TenNhomTaiKhoan)
                {
                    kt = false;
                    txtTenNhomHang.Text = sosanh.TenNhomTaiKhoan;
                    MessageBox.Show(Common.Constants.GiaTriCacForm.NhapTenNhomTKKTTD, Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    return kt;
                }
                if (manggoc.GhiChu != sosanh.GhiChu)
                {
                    kt = false;
                    txtGhiChu.Text = sosanh.GhiChu;
                    MessageBox.Show(Common.Constants.GiaTriCacForm.GhiChuThayDoi, Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    return kt;
                }
                if (manggoc.Deleted != sosanh.Deleted)
                {
                    kt = false;
                    MessageBox.Show(Common.Constants.GiaTriCacForm.DeleteThayDoi, Common.Constants.GiaTriCacForm.DeleteThayDoi);
                    return kt;
                }
            }
            catch 
            {  kt = false; }
            return kt;
        }
        #endregion

        #region Xu Ly
        private Entities.NhomTKKeToan XuLyDuLieu(string hanhdong)
        {
            Entities.NhomTKKeToan xuly = null;
            try
            {
                xuly = new Entities.NhomTKKeToan();
                xuly.HanhDong = hanhdong;
                xuly.NhomTKKeToanID = this.NhomTKKeToanID;
                xuly.MaNhomTKKeToan = txtMaNhomHang.Text.ToString();
                xuly.TenNhomTaiKhoan = txtTenNhomHang.Text.ToString();
                xuly.GhiChu = txtGhiChu.Text;
                xuly.Deleted = false;
                xuly.TaiKhoan = UtilitiesVariable.User;
            }
            catch 
            {  xuly = null; }
            return xuly;
        }
       
        private void InsertUpdate(string hanhdong)
        {
            try
            {
                Entities.NhomTKKeToan nh = XuLyDuLieu(hanhdong);
                if (Validate(nh) != true)
                {
                    return;
                }
                int i = Common.StaticVariable.InterfaceObject.InsertUpdateNhomTKKeToan(nh);

                if (i != 0)
                {
                     
                    if (hanhdong == Common.Constants.GiaTriCacForm.Insert)
                    {
                        CheckClose = true;
                        this.Close();
                    }
                    else
                    {
                        CheckClose = false;
                        kiemtrasua = true;
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show(Common.Constants.GiaTriCacForm.ThatBai, Common.Constants.GiaTriCacForm.HeThongCanhBao);
                }
            }
            catch 
            {  }
        }
        #endregion

        #region Them
        private Boolean getData(string ma)
        {
            Boolean kt = true;
            try
            {
                Entities.NhomTKKeToan nhom = new Entities.NhomTKKeToan("0");
                Entities.NhomTKKeToan[] ntk = Common.StaticVariable.InterfaceObject.SelectNhomTKKeToan(nhom);

                for (int k = 0; k < ntk.Length; k++)
                {
                    if (ntk[k].MaNhomTKKeToan == ma)
                    { kt = false; break; }
                    else
                    { continue; }
                }
            }
            catch 
            {  kt = false; }
            return kt;
        }

        private void Them()
        {
            try
            {
                if (CheckPerMission() != true)
                {
                    return;
                }
                if (this.getData(txtMaNhomHang.Text) != true)
                {
                    MessageBox.Show(Common.Constants.GiaTriCacForm.DaCoMa, Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    txtMaNhomHang.Focus();
                    return;
                }
                InsertUpdate(Common.Constants.GiaTriCacForm.Insert);
                CheckClose = true; kiemtrasua = true;
            }
            catch 
            {  }
        }
        #endregion

        #region Sua
        private void Sua()
        {
            try
            {
                if (CheckPerMission() != true)
                {
                    return;
                }
                // check conflic update
                if (CheckConflick(this.nhomTKKT, SoSanh()) != true)
                {
                    return;
                }
                // chech update
                InsertUpdate(Common.Constants.GiaTriCacForm.Update);
            }
            catch 
            {  }
        }
        #endregion

        #region Thoat
        private void frmXuLyNhomHangHoa_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                if (CheckClose == false &&  kiemtrasua == false)
                {
                    System.Windows.Forms.DialogResult giatri = System.Windows.Forms.MessageBox.Show(Common.Constants.GiaTriCacForm.ThongBaoThoat, Common.Constants.GiaTriCacForm.HeThongCanhBao, System.Windows.Forms.MessageBoxButtons.YesNo);
                    {
                        if (giatri == System.Windows.Forms.DialogResult.Yes)
                        {

                            e.Cancel = CheckClose;
                        }
                        else
                        {
                            e.Cancel = !CheckClose;
                        }
                    }
                }
                GetData(true);
            }
            catch 
            {  }
        }
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult giatri = System.Windows.Forms.MessageBox.Show(Common.Constants.GiaTriCacForm.ThongBaoThoat, Common.Constants.GiaTriCacForm.HeThongCanhBao, System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (giatri == System.Windows.Forms.DialogResult.Yes)
                    {
                         
                        kiemtrasua = true;
                        CheckClose = false;
                        this.Close();
                    }
                }
            }
            catch 
            {  }
        }
        #endregion

    }
}
